﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

using FCNS.KeepMoney.Configuration;
using FCNS.KeepMoneyLibrary.Data;

namespace FCNS.KeepMoney.Forms
{
    public partial class MsgForm : FCNS.KeepMoney.Forms.ParentForm
    {
        struct TX
        {
           public string text, url;
            public TX(string t, string u)
            {
                text = t;
                url = u;
            }
        }

        List<TX> allTx = new List<TX>();
        int index = 0;

        public void Add(EntryField field)
        {
            StringBuilder sb = new StringBuilder(field.columnName);
            sb.Append(" ( ");
            sb.Append(field.columnNote);
            sb.Append(" \r\n) ");
            sb.Append(field.columnMoney);
            sb.Append("\r\n");
            sb.Append(Util.LanguageUtil.于 + "\r\n");
            sb.Append(field.columnAlertDate);
            sb.Append(Util.LanguageUtil.账务到期提醒);

            allTx.Add(new TX(sb.ToString(), string.Empty));
        }

        public void Add(string text, string url)
        {
            allTx.Add(new TX(text, url));
        }

        private void LoadHelpText()
        {
            this.Text = "╭∩╮（︶︿︶）╭∩╮";
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(AppDefines.appPath + "help.xml");
                foreach (XmlNode node in xml.SelectNodes("Help/KM/Item"))
                    allTx.Add(new TX(node.SelectSingleNode("Text").InnerText,
                        node.SelectSingleNode("Url").InnerText));
            }
            catch
            {
                allTx.Add(new TX("程序帮助文件出现异常,请点击底部按钮获取更多信息...", "http://www.keepmoney.org/help/use/error.html#help"));
            }
        }

        public MsgForm()
        {
            InitializeComponent();
        }

        private void MsgForm_Load(object sender, EventArgs e)
        {
            if (allTx.Count == 0)
                LoadHelpText();

            SetTX();

            buttonPre.Enabled = false;
            if (allTx.Count > 1)
                return;

            buttonNext.Enabled = false;
        }

        private void buttonPre_Click(object sender, EventArgs e)
        {
            index -= 1;
            buttonNext.Enabled = true;
            buttonPre.Enabled = true;

            if (index == 0)
                buttonPre.Enabled = false;

            SetTX();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            index += 1;
            buttonNext.Enabled = true;
            buttonPre.Enabled = true;

            if (index == allTx.Count - 1)
                buttonNext.Enabled = false;

            SetTX();
        }

        private void SetTX()
        {
            labelText.Text = allTx[index].text;
            if (allTx[index].url.Length > 5)
                linkLabelMore.Visible = true;
            else
                linkLabelMore.Visible = false;
        }

        private void linkLabelMore_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string str = allTx[index].url;
            if (str.Length > 5)
                System.Diagnostics.Process.Start(str);
        }
    }
}

